package com.example.text8;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllVideoActivity extends AppCompatActivity {
    @BindView(R.id.list_video)
    ListView list_video;

    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_video);
        unbinder = ButterKnife.bind(AllVideoActivity.this);

        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        if (unbinder != null) {
            unbinder.unbind();
        }

        super.onDestroy();
    }

    private void initView() {
        list_video.setOnItemClickListener((parent, view, position, id) -> {
            HashMap<String, String> hashMap = (HashMap<String, String>) list_video.getItemAtPosition(position);    //ListView点击信息map
            String videoPlay = hashMap.get("path");  //视频绝对路径

            Intent intent = new Intent(AllVideoActivity.this, PlayVideoActivity.class)
                    .putExtra(PlayVideoActivity.VIDEO_PATH, videoPlay);
            startActivity(intent);
        });
    }

    private void initData() {
        Cursor cursor = getContentResolver()
                .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        new String[]{MediaStore.Video.Media.TITLE,
                                MediaStore.Video.Media.DURATION,
                                MediaStore.Video.Media._ID,
                                MediaStore.Video.Media.DISPLAY_NAME,
                                MediaStore.Video.Media.DATA},
                        null, null, null);

        if (cursor == null || cursor.getCount() == 0) {//如果没有搜索到视频，显示存储列表为空...
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("存储列表为空...").setPositiveButton("确定", null);
            AlertDialog ad = builder.create();
            ad.show();
        } else {  //数据不为空将其展现在ListView上
            ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
            //将数据存放在arrayList中
            while (cursor.moveToNext()) {
                HashMap<String, String> map = new HashMap<>();
                //要播放的视频文件名
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
                //呈现出来的歌曲信息
                String data = "文件名:" + name
                        + " ,TITLE:" + cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE))
                        + " ,ID=" + cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));
                //视频时长
                String time = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)) / 1000 + "s";
                //播放视频的绝对路径，存入map中但不显示在listview中，为了在点击事件中获取到点击位置要播放视频的path
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));

                map.put("data", data);
                map.put("time", time);
                map.put("path", path);
                arrayList.add(map);
            }

            SimpleAdapter simpleAdapter = new SimpleAdapter(AllVideoActivity.this,
                    arrayList,
                    R.layout.list_view_item,
                    new String[]{"data", "time"},
                    new int[]{R.id.tv_data, R.id.tv_time});
            list_video.setAdapter(simpleAdapter);
        }

        if (cursor != null) {
            cursor.close();
        }
    }
}